<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Alert dialog | Onsen UI</title>
  <link rel="stylesheet" href="../../css/src/onsenui.css">
  <link rel="stylesheet" href="../../css/css-components-src/onsen-css-components.css">

  <script src="../../js/onsenui.js"></script>

  <script>
    app = { alertDialog: null };

    ons.ready(function() {
      ons.createAlertDialog('alert.html').then(function(element) {
        app.alertDialog = element;
      });
    });

    app.fromTemplate = function() {
      app.alertDialog.show();
    };

    app.close = function() {
      if(app.alertDialog && app.alertDialog.visible) {
        app.alertDialog.hide();
      }
    };

    app.alert = function() {
      ons.notification.alert('Hello, world!');
    };

    app.confirm = function() {
      ons.notification.confirm({
        message: 'Do you like ramen?',
        buttonLabels: ['Yes', 'No'],
        callback: function(i) {
          if (i==0) {
            ons.notification.alert('Me too!');
          } else {
            ons.notification.alert('That\'s too bad...');
          }
        }
      });
    };

    app.prompt = function() {
      var fn = function() {
        ons.notification.prompt({
          message: 'What is the meaning of Life, the Universe and Everything?',
          callback: function(answer) {
            if (answer === "42") {
              ons.notification.alert('That\'s the correct answer!');
            } else {
              ons.notification.alert({
                message: 'Incorrect! Please try again!',
                callback: fn
              });
            }
          }
        });
      };

      fn();
    };

    app.cancelable = function() {
      ons.notification.confirm({
        message: 'This dialog can be canceled by tapping the background or using the back button on your device.',
        cancelable: true,
        callback: function(i) {
          if (i == -1) {
            ons.notification.alert('You canceled it!');
          }
        }
      });
    };
  </script>

</head>

<body>
  <ons-page>
    <ons-toolbar>
      <div class="center">Alert dialog</div>
    </ons-toolbar>

    <p style="text-align: center">
      <ons-button onclick="app.fromTemplate()">Create from template</ons-button>
      <ons-button onclick="app.alert()">Alert</ons-button>
      <ons-button onclick="app.confirm()">Confirm</ons-button>
      <ons-button onclick="app.prompt()">Prompt</ons-button>
      <ons-button onclick="app.cancelable()">Cancelable</ons-button>
    </p>
  </ons-page>

  <template id="alert.html">
    <ons-alert-dialog modifier="rowfooter">
      <div class="alert-dialog-title">Warning!</div>
      <div class="alert-dialog-content">
        An error has occurred!
      </div>
      <div class="alert-dialog-footer">
        <ons-alert-dialog-button onclick="app.close()">Cancel</ons-alert-dialog-button>
        <ons-alert-dialog-button onclick="app.close()">OK</ons-alert-dialog-button>
        <!-- Old way:
        <button class="alert-dialog-button" onclick="app.close()">Cancel</button>
        <button class="alert-dialog-button" onclick="app.close()">OK</button>
        -->
      </div>
    </ons-alert-dialog>
  </template>

</body>
</html>
